package parameters;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/** Implements a connection data container and can be used to
 * save information to a .cmng file
 * 
 * @author Konstantin Dobrev
 * @version 1.0
 */
public class ConnectionParams implements Serializable {
	
	private static final long serialVersionUID = 1L;
	private String port;
	private String dbType;
	private String password;
	private String username;
	private String connectionName;
	private boolean savedPass;
	
	/** Stores port data
	 * 
	 * @param portNum stores port id
	 */
	public void setPort(String portNum) { port = portNum; }
	
	/** Stores database system type (MySQL, MS SQL Server...)
	 * 
	 * @param type stores the type
	 */
	public void setDbType(String type) { dbType = type; }
	
	/** Stores password data
	 * 
	 * @param stores the password
	 */
	public void setPassword(String pass) { password = pass; }
	
	/** Stores username data
	 * 
	 * @param user stores the username
	 */
	public void setUsername(String user) { username = user; }
	
	/** Stores
	 * 
	 * @param name
	 */
	public void setConnectionName(String name) { connectionName = name; }
	
	/** Stores information about password 
	 * 
	 * @param state stores the information
	 */
	public void savePass(boolean state) { savedPass = state; }
	
	/** Simple getter for port data
	 * 
	 * @return port number as String
	 */
	public String getPort() { return port; }
	
	/** Simple getter for database type
	 * 
	 * @return database type as String
	 */
	public String getDbType() { return dbType; }
	
	/** Simple getter for password data
	 * 
	 * @return password as a String
	 */
	public String getPassword() { return password; }
	
	/** Simple getter for the username
	 * 
	 * @return username as String
	 */
	public String getUsername() { return username; }
	
	/** Simple getter for the connection file name
	 * 
	 * @return file name as String
	 */
	public String getConnectionName() { return connectionName; }
	
	/** Simple getter for the password state
	 * 
	 * @return password check state as boolean
	 */
	public boolean isPassSaved() { return savedPass; }
	
	/** Establishes a connection to a database 
	 * 
	 * @return the result connection object reference
	 * @throws SQLException
	 */
	public Connection createConnection() throws SQLException {
		String url = "jdbc:" + dbType + "://localhost:" + port + "/";
		Connection conn = DriverManager.getConnection(url, username, password);
		return conn;
	}
	
}
